﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GingerGL.IO
{
    //---------------------------------------------------------------//
    /// <summary>
    /// Standard File System
    /// </summary>
    public class FileSystem : IFileSystem
    {
        //-----------------------------------------------------------//
        #region Variables
        private string m_path = String.Empty;
        #endregion
        //-----------------------------------------------------------//
        #region Properties
        /// <summary>
        /// Gets Folder Root Path
        /// </summary>
        public string Root
        {
            get { return m_path; }
            set
            {
                m_path = value;
                m_path = Path.Unify(value);
            }
        }
        #endregion
        //-----------------------------------------------------------//
        #region Constructors
        /// <summary>
        /// Initailizes a new instance of object
        /// </summary>
        public FileSystem(string root)
        {
            Root = root;
        }
        #endregion
        //-----------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Opens File
        /// </summary>
        public Stream Open(string fileName)
        {
            Stream retStream = new FileStream(GetFullPath(fileName), FileMode.Open);
            return retStream;
        }
        /// <summary>
        /// Saves File
        /// </summary>
        public void Save(Stream stream, string fileName)
        {
            Stream fileStream = new FileStream(GetFullPath(fileName), FileMode.Create);
            byte[] data = new byte[stream.Length];
            stream.Read(data, 0, (int)stream.Length);
            fileStream.Write(data, 0, (int)stream.Length);
        }
        /// <summary>
        /// Returns True if file exists
        /// </summary>
        public bool Exists(string fileName)
        {
            FileInfo fileInfo = new FileInfo(GetFullPath(fileName));
            return fileInfo.Exists;
        }
        /// <summary>
        /// Gets Fullpath
        /// </summary>
        public string GetFullPath(string fileName)
        {
            return Path.Merge(m_path, fileName);
        }
        /// <summary>
        /// Gets a list of files
        /// </summary>
        public string[] GetFiles(string path)
        {
            return Path.Unify(Directory.GetFiles(GetFullPath(path)));
        }
        /// <summary>
        /// Gets a list of directories
        /// </summary>
        public string[] GetDirectories(string path)
        {
            return Path.Unify(Directory.GetDirectories(GetFullPath(path)));
        }
        /// <summary>
        /// Gets a list of files
        /// </summary>
        public string[] GetFiles(string path, string pattern)
        {
            return Path.Match(GetFiles(path), pattern);
        }
        /// <summary>
        /// Gets a list of directories
        /// </summary>
        public string[] GetDirectories(string path, string pattern)
        {
            return Path.Match(GetDirectories(path), pattern);
        }
        #endregion
        //-----------------------------------------------------------//
    }
    //---------------------------------------------------------------//
}
